package com.ft.oa.service.customer.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.customer.CustomerSupplier;
import com.ft.oa.domain.dto.knowledgeBase.AttachmentDto;
import com.ft.oa.enums.DirectoryTypeEnum;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.customer.CustomerSupplierMapper;
import com.ft.oa.service.customer.CustomerSupplierService;
import com.ft.oa.service.knowledgeBase.DirectoryService;
import com.ft.oa.utils.SecurityUtils;
import com.ft.oa.web.customer.rq.CustomerSupplierPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CustomerSupplierServiceImpl implements CustomerSupplierService {

    @Resource
    private CustomerSupplierMapper customerSupplierMapper;

    @Resource
    private DirectoryService directoryService;

    /**
     * 分页查询供应商资料
     *
     * @param
     * @param
     * @return
     */
    @Override
    public Page<CustomerSupplier> selectCustomerSupplier(CustomerSupplierPage customerSupplierPage) {
        Page<CustomerSupplier> pages = new Page<>(customerSupplierPage.getPage(),customerSupplierPage.getSize());
        QueryWrapper<CustomerSupplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",0);
        if (customerSupplierPage.getSupplierName() !=null&&!"".equals(customerSupplierPage.getSupplierName())){
            queryWrapper.like("supplier_name",customerSupplierPage.getSupplierName());
        }
        if (customerSupplierPage.getImportance() !=null){
            queryWrapper.eq("importance",customerSupplierPage.getImportance());
        }
        if (customerSupplierPage.getCooperation() !=null){
            queryWrapper.eq("cooperation",customerSupplierPage.getCooperation());
        }
        if(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager())){
            queryWrapper.eq("creator",SecurityUtils.getCurrentUser().getId());
        }
        queryWrapper.select().orderByDesc("update_time");
        return customerSupplierMapper.selectPage(pages,queryWrapper);
    }

    /**
     * 添加供应商信息
     *
     * @param customerSupplier
     * @return
     */
    @Override
    public int insertCustomerSupplier(CustomerSupplier customerSupplier) {

        List<AttachmentDto> attachments = customerSupplier.getAttachments();
        if (attachments != null&&attachments.size()>0){
            String jsonString = JSONArray.toJSONString(attachments);
            customerSupplier.setAchment(jsonString);
        }
        customerSupplier.setState(0);
        int insert = customerSupplierMapper.insert(customerSupplier);

        /**
         * 保存附件项目相关的公共方法
         *
         * @param attachments 附件集合
         * @param source      来源  根据 DirectoryTypeEnum
         * @param relationId  业务id
         * @param sourceId    如果一个表单有多个附件，用于标识附件属于表单的哪个附件
         * @param  projectNumber 项目编号 全局唯一
         */
        directoryService.saveProAttachment(attachments, DirectoryTypeEnum.FILING_SUPPLIER.getCode(),String.valueOf(customerSupplier.getId()),"1","null");
        return insert;
    }

    /**
     * 修改供应商信息
     * @param customerSupplier
     * @return
     */
    @Override
    public int updateCustomerSupplier(CustomerSupplier customerSupplier) {
     return    customerSupplierMapper.updateById(customerSupplier);
    }

    /**
     * 删除供应商信息
     *
     * @param
     * @return
     */
    @Override
    public int deleteCustomerSupplier(CustomerSupplier customerSupplier) {
        CustomerSupplier supplier = customerSupplierMapper.selectById(customerSupplier.getId());
        if (supplier.getState() == 0){
            supplier.setState(1);
            return customerSupplierMapper.updateById(supplier);
        }
      return 0;
    }

    /**
     * 查询所有供应商信息
     * @return
     */
    @Override
    public List<CustomerSupplier> select() {
        QueryWrapper<CustomerSupplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",0);
        List<CustomerSupplier> customerSuppliers = customerSupplierMapper.selectList(queryWrapper);
        return customerSuppliers;
    }

    /**
     * 根据ID查询供应商详情
     *
     * @param customerSupplier
     * @return
     */
    @Override
    public CustomerSupplier selectById(CustomerSupplier customerSupplier) {
       return customerSupplierMapper.selectById(customerSupplier.getId());
    }

}
